/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header: d:/cvshome/repository/src/net/risesoft/commons/event/eventlistner/LogEventListner.java,v 1.1 2008/01/06 06:48:14 administrator Exp $
 */
package cn.newtest.bi.collection.event.eventlistener;

import java.util.Map;

import net.risesoft.risebi.collection.event.eventobj.DbRegEventObj;
import net.risesoft.risebi.collection.service.EventListenerManager;
import net.risesoft.risebi.commons.util.ContextUtil;
import net.risesoft.risebi.commons.util.ExceptionUtil;
import net.risesoft.risebi.commons.util.ThreadPoolUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * 
 * @author <a href="mailto: baiyibing@gmail.com">王锤</a>
 * @version $Revision: 1.1 $
 * 
 */
@Component
public class DbRegEventListener implements ApplicationListener {

	private static Log log = LogFactory.getLog(DbRegEventListener.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.context.ApplicationListener#onApplicationEvent(org
	 * .springframework.context.ApplicationEvent)
	 */
	public void onApplicationEvent(ApplicationEvent evt) {
		if (evt instanceof DbRegEventObj) {
			DbRegEventTask let = new DbRegEventTask((DbRegEventObj) evt);
			try {
				ThreadPoolUtil.getInstance().execute(let);
			} catch (Exception e) {
				log.error(ExceptionUtil.transformException(e));
			}
		}else{
			log.debug(evt.getClass());
		}
	}

	public class DbRegEventTask implements Runnable {

		private DbRegEventObj eo;

		public DbRegEventObj getEo() {
			return eo;
		}

		public void setEo(DbRegEventObj eo) {
			this.eo = eo;
		}

		public DbRegEventTask() {

		}

		public DbRegEventTask(DbRegEventObj eo) {
			this.eo = eo;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see java.lang.Runnable#run()
		 */
		public void run() {
			log.debug("开始线程");
			String eventName = eo.getEventName();
			try {
				if (eventName != null) {
					log.debug("执行数据库注册事件处理程序");
					EventListenerManager eventListenerManager = (EventListenerManager) ContextUtil
							.getAc().getBean("eventListenerManager");
					Map dataMap = eo.getDataMap();
					eventListenerManager.onDbRegEvent(eventName, dataMap);
				} else {
					log.debug("没有找到事件命名");
				}
			} catch (Exception e) {
				log.error(ExceptionUtil.transformException(e));
			}
		}

	}

}